package com.mmdccj.yugu.service.impl;

import com.mmdccj.yugu.entity.Post;
import com.mmdccj.yugu.entity.User;
import com.mmdccj.yugu.mapper.UserMapper;
import com.mmdccj.yugu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: star_
 * @date: 2023-11-25 17:26
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    //自动注入UserMapper实例
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据账户查询用户
     *
     * @param account 账号
     * @return User
     */
    @Override
    public User getUserByAccount(String account) {
        return userMapper.getUserByAccount(account);
    }

    /**
     * 根据id获取用户
     *
     * @param id 用户id
     * @return User
     */
    @Override
    public User getUserById(String id) {
        return userMapper.getUserById(id);
    }

    /**
     * 根据邮箱获取用户
     *
     * @param email 邮件
     * @return User
     */
    @Override
    public User getUserByEmail(String email) {
        return userMapper.getUserByEmail(email);
    }

    /**
     * 判断某个账号是否已注册
     *
     * @param account 账号
     * @return boolean
     */
    @Override
    public boolean isUser(String account) {
        return userMapper.isUser(account) > 0;
    }

    /**
     * 通过用户id获取用户名
     *
     * @param id 用户id
     * @return string 用户名
     */
    @Override
    public String getUserNameById(String id) {
        return userMapper.getUserNameById(id);
    }

    /**
     * 通过音乐id获取用户信息
     *
     * @param id 音乐id
     * @return User 用户对象
     */
    @Override
    public User getUserByMusicId(String id) {
        String[] userId = userMapper.getUserIdByMusicId(id);
        if (userId.length == 0) {
            return null;
        }
        log.info("获取到id:{}", id);
        return this.getPublicUserInfo(userId[0]);
    }

    @Override
    public boolean updateUserInfo(String id, String name, String introduce) {
        int raw = userMapper.updateUserInfo(id, name, introduce);
        return raw == 1;
    }

    @Override
    public List<User> getCommentsUsers(List<Post> post) {
        return userMapper.getCommentsUsers(post);
    }

    @Override
    public List<User> getUsersByIds(String[] ids) {
        return userMapper.getUsersByIds(ids);
    }

    @Override
    public User getPublicUserInfo(String userId) {
        return userMapper.getPublicUserInfo(userId);
    }

    @Override
    public List<String> getUserIdsByMusicIds(List<String> ids) {
        return userMapper.getUserIdsByMusicIds(ids);
    }

    @Override
    public List<User> getPublicUserListInfoByUserIds(List<String> ids) {
        return userMapper.getPublicUserInfoListByUserIds(ids);
    }

    /**
     * 获取用户头像
     *
     * @param userId userId
     * @return 用户头像url
     */
    @Override
    public String getAvatar(String userId) {
        return userMapper.getAvatar(userId);
    }

    /**
     * 更新用户头像
     *
     * @param id 用户id
     * @return 更新结果
     */
    @Override
    public boolean updateAvatar(String id, String avatar) {
        return userMapper.updateAvatar(avatar, id) > 0;
    }

    /**
     * 通过歌单id获取收藏该歌单的用户
     *
     * @param musicListId 歌单id
     * @return List<User>
     */
    @Override
    public List<User> getCollectUsers(String musicListId) {
        List<String> userIds = userMapper.getCollectUserIdsByMusicListId(musicListId);
        if (userIds.size() == 0) {
            return null;
        }
        return userMapper.getPublicUserInfoListByUserIds(userIds);
    }

    @Override
    public boolean isPhone(String phone) {
        return userMapper.isPhone(phone) > 0;
    }


    /**
     * 在用户表中插入数据
     *
     * @param user 用户
     */
    @Override
    public void insertUser(User user) {
        userMapper.insertUser(user);
    }


}
